
// Definition for singly-linked list.
function ListNode(val) {
    this.val = val;
    this.next = null;
}


/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function(headA, headB) {
  let lengthA = 0, lengthB = 0, tempA = headA, tempB = headB;
  while(tempA) {
    lengthA++;
    tempA = tempA.next;
  }
  while(tempB) {
    lengthB++;
    tempB = tempB.next;
  }
  tempA = headA, tempB = headB;
  while(lengthA > 0 && lengthB > 0 && lengthA != lengthB) {
    if(lengthA > lengthB) {
      tempA = tempA.next;
      lengthA--;
    } else {
      tempB = tempB.next;
      lengthB--;
    }
  }
  if(!lengthA || !lengthB) return null;
  while(tempA && tempB) {
    if(tempA == tempB) return tempA
    tempA = tempA.next;
    tempB = tempB.next;
  }
  return null;
};
let intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3;
console.log(getIntersectionNode(headA, headB));
intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1;
console.log(getIntersectionNode(headA, headB));
intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2;
console.log(getIntersectionNode(headA, headB));
